<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.memberNickName"
          clearable
          placeholder="用户昵称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.productName"
          clearable
          placeholder="产品名"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.showStatus"
          clearable
          placeholder="是否展示"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        row-key="id"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="memberIcon" label="用户昵称"       width="120px">
          <template slot-scope="scope">
            <div class="productInfo">
              <img :src="baseApi + scope.row.memberIcon" class="tableImg" alt="">
              <span>{{ scope.row.memberName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="產品名"       width="200px" show-tooltip-when-overflow>
          <template slot-scope="scope">
            <div class="productInfo">
              <img :src="baseApi + scope.row.pic" class="tableImg" alt="">
              <span>{{ scope.row.productName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="內容"       show-tooltip-when-overflow />
        <el-table-column prop="star" label="星級"       />
        <el-table-column prop="pics" label="上傳圖片"      >
          <template slot-scope="scope">
            <img v-for="(pic, index) in scope.row.pics" :key="index" :src="baseApi + pic" style="width: 30px" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="showStatus" label="是否展示"      >
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.sysStatus"
              :row-data="scope.row.showStatus"
              row-filed="showStatus"
              :title="scope.row.nameCn"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudComment from '@/api/pms/comment'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import mallSwitch from '@/components/mallSwitch'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

const defaultForm = {
  id: null,
  memberIcon: null,
  memberNickName: null,
  content: null,
  pics: null,
  showStatus: 0,
  memberName: null
}
export default {
  name: 'Comment',
  components: { pagination, crudOperation, rrOperation, mallSwitch },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      imageUrl: '',
      type: { type: 3 },
      headers: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'comment:add'],
        edit: ['admin', 'comment:edit'],
        del: ['admin', 'comment:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'memberNickName', display_name: '用户昵称' },
        { key: 'productId', display_name: '产品id' },
        { key: 'showStatus', display_name: '是否展示' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  dicts: ['sysStatus'],
  cruds() {
    return CRUD({
      title: '评论', url: 'comment/page', sort: 'id:desc', crudMethod: { ...crudComment },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.imageUrl = form.pic
    },
    [CRUD.HOOK.afterEditCancel]() {
      this.imageUrl = ''
    },
    [CRUD.HOOK.afterAddCancel]() {
      this.imageUrl = ''
    },
    [CRUD.HOOK.afterSubmit]() {
      this.imageUrl = ''
    },
    handleSuccess(res, file) {
      if (res.code === 0) {
        this.form.memberIcon = this.baseApi + res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    handleError() {
      this.$message.error('图片上传失败,请重新上传')
    },
    clearFiles() {
      this.$refs['my-upload'].clearFiles()
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.productInfo span {
  display: block;
  line-height: 16px;
}

.tableImg {
  width: 30px;
  margin-right: 0px;
  float: left;
}
</style>
